package com.xr.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xr.blog.pojo.BlogLog;
import com.xr.blog.service.BlogLogService;
import com.xr.blog.tools.Result;
import com.xr.blog.tools.constant.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 日志表
 *
 * @author XRJ
 * @email 1033835209@qq.com
 * @date 2020-07-07 20:31:38
 */
@RestController
@RequestMapping("/squirrelblog")
public class BlogLogController {
    @Autowired
    private BlogLogService blogLogService;


    /**
     * 统计访客的浏览器数据
     * @return
     */
    @GetMapping(value = "/log/browser",produces = "application/json;charset=UTF-8")
    public Result<Map<String,Integer>> browser(){
        return Result.of(Code.SC_OK.getState(),"获取成功",blogLogService.statBrowser());
    }


    /**
     * 统计访客的操作系统
     * @return
     */
    @GetMapping(value = "/log/operatingSystem",produces = "application/json;charset=UTF-8")
    public Result<Map<String,Integer>> operatingSystem(){
        return Result.of(Code.SC_OK.getState(),"获取成功",blogLogService.statOperatingSystem());
    }

    /**
     * 统计访客的所在城市
     * @return
     */
    @GetMapping(value = "/log/city",produces = "application/json;charset=UTF-8")
    public Result<List<Map<String, Integer>>> city(){
        return Result.of(Code.SC_OK.getState(),"获取成功",blogLogService.statCity());
    }

    /**
     * 获得最近一周访客数和总访客数
     * @return
     */
    @GetMapping(value = "/log/visitsCount",produces = "application/json;charset=UTF-8")
    public Result<Map<String,Integer>> visitsCount(){
        Map<String,Integer> data=new HashMap<>();
        int totalVisits= blogLogService.count();
        data.put("totalVisits",totalVisits);
        int latestVisits=blogLogService.count(new QueryWrapper<BlogLog>().apply("create_time > DATE_SUB(CURDATE(), INTERVAL 1 WEEK)"));
        data.put("latestVisits",latestVisits);
        return Result.of(Code.SC_OK.getState(),"获取成功",data);
    }

    /**
     * 获得最新的n条最新日志
     * @return
     */
    @GetMapping(value = "/log/latest",produces = "application/json;charset=UTF-8")
    public Result<List<BlogLog>> latest(int number){
        List<BlogLog> logList=blogLogService.findLatestLog(number);
        return Result.of(Code.SC_OK.getState(),"获取成功",logList);
    }
}
